package io.gitee.yxsnake.embark.web.core.context;

import io.gitee.yxsnake.embark.web.core.base.IBaseEnum;
import io.gitee.yxsnake.embark.web.core.context.model.UserInfoDTO;
import io.gitee.yxsnake.embark.web.core.enums.*;

import java.util.Objects;
import java.util.Set;

/**
 * @author: snake
 * @create-time: 2024-09-29
 * @description: 用户上下文信息管理
 * @version: 1.0
 */
public class UserContext {

    private static ThreadLocal<UserInfoDTO> threadLocal;

    private UserContext() {
        threadLocal = new ThreadLocal<>();
    }

    /**
     * 创建实例
     *
     * @return
     */
    public static UserContext getInstance() {
        return SingletonContext.userContext;
    }

    /**
     * 静态内部类单例模式
     * 单例初使化
     */
    private static class SingletonContext {
        private static final UserContext userContext = new UserContext();
    }

    /**
     * 用户上下文中放入信息
     *
     * @param userInfoDTO
     */
    public static void setContext(UserInfoDTO userInfoDTO) {
        threadLocal.set(userInfoDTO);
    }

    /**
     * 获取上下文中的信息
     *
     * @return
     */
    public static UserInfoDTO getContext() {
        return threadLocal.get();
    }
    /**
     * 获取上下文中的用户id
     *
     * @return
     */
    public static String getUserId() {
        return getContext().getUserId();
    }

    /**
     * 获取上下文中的账号
     *
     * @return
     */
    public static String getAccount() {
        return getContext().getAccount();
    }

    /**
     *  获取上下文中的账号ID
     * @return
     */
    public static String getAccountId() {
        return getContext().getAccountId();
    }

    /**
     * 获取手机号码
     * @return
     */
    public static String getPhone(){
        return getContext().getPhone();
    }

    /**
     * 获取昵称
     * @return
     */
    public static String getNickname(){
        return getContext().getNickname();
    }

    /**
     * 获取真实姓名
     * @return
     */
    public static String getRealName(){
        return getContext().getRealName();
    }

    /**
     * 获取头像
     * @return
     */
    public static String getAvatar(){
        return getContext().getAvatar();
    }

    /**
     * 获取性别枚举实例
     * @return
     */
    public static GenderEnum getGender(){
        Integer gender = getContext().getGender();
        GenderEnum genderEnum = IBaseEnum.getEnumByValue(gender, GenderEnum.class);
        return genderEnum;
    }

    /**
     * 判断当前用户是否超级管理员
     * @return
     */
    public static Boolean isSupperAdmin(){
        AccountTypeEnum accountTypeEnum = getAccountType();
        if(!AccountTypeEnum.EMP.equals(accountTypeEnum)){
            return false;
        }
        Integer isSupperAdmin = getContext().getIsSupperAdmin();
        if (Objects.nonNull(isSupperAdmin) && SupperAdminEnum.YES.equals(isSupperAdmin)){
            return true;
        }
        return false;
    }

    /**
     * 获取租户ID
     * @return
     */
    public static String getTenantId(){
       return getContext().getTenantId();
    }

    /**
     * 获取当前用户的角色id集合
     * @return
     */
    public static Set<String> getCurrentUserRoleIds(){
        return getContext().getRoleIds();
    }

    /**
     * 获取当前用户的角色编码集合
     * @return
     */
    public static Set<String> getCurrentUserRoleCodes(){
        return getContext().getRoleCodes();
    }

    /**
     * 获取当前用的权限ID集合
     * @return
     */
    public static Set<String> getCurrentUserPermissions(){
        return getContext().getPermissionIds();
    }

    /**
     * 登录方式枚举实例
     * @return
     */
    public static LoginWayEnum getLoginWay(){
        Integer loginWay = getContext().getLoginWay();
        LoginWayEnum loginWayEnum = IBaseEnum.getEnumByValue(loginWay, LoginWayEnum.class);
        return loginWayEnum;
    }

    /**
     * 获取账号类型
     * @return
     */
    public static AccountTypeEnum getAccountType(){
        Integer accountType = getContext().getAccountType();
        AccountTypeEnum accountTypeEnum = IBaseEnum.getEnumByValue(accountType, AccountTypeEnum.class);
        return accountTypeEnum;
    }

    public static ChannelTypeEnum getChannelType(){
        String channelType = getContext().getChannelType();
        ChannelTypeEnum channelTypeEnum = IBaseEnum.getEnumByValue(channelType, ChannelTypeEnum.class);
        return channelTypeEnum;
    }
    /**
     * 清空上下文
     */
    public static void clear() {
        threadLocal.remove();
    }

}
